{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电影名称</th>\n",
       "      <th>打斗镜头</th>\n",
       "      <th>接吻镜头</th>\n",
       "      <th>电影类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>California Man</td>\n",
       "      <td>3</td>\n",
       "      <td>104</td>\n",
       "      <td>爱情片</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>He's Not Really into Dudes</td>\n",
       "      <td>2</td>\n",
       "      <td>100</td>\n",
       "      <td>爱情片</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Beautiful Woman</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>爱情片</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Kevin Longblade</td>\n",
       "      <td>101</td>\n",
       "      <td>10</td>\n",
       "      <td>动作片</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Robo Slayer 3000</td>\n",
       "      <td>99</td>\n",
       "      <td>5</td>\n",
       "      <td>动作片</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Amped II</td>\n",
       "      <td>98</td>\n",
       "      <td>2</td>\n",
       "      <td>动作片</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         电影名称  打斗镜头  接吻镜头 电影类型\n",
       "0              California Man     3   104  爱情片\n",
       "1  He's Not Really into Dudes     2   100  爱情片\n",
       "2             Beautiful Woman     1    81  爱情片\n",
       "3             Kevin Longblade   101    10  动作片\n",
       "4            Robo Slayer 3000    99     5  动作片\n",
       "5                    Amped II    98     2  动作片"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\n",
    "    '电影名称': ['California Man', 'He\\'s Not Really into Dudes', 'Beautiful Woman', 'Kevin Longblade', 'Robo Slayer 3000', 'Amped II'],\n",
    "    '打斗镜头': [3, 2, 1, 101, 99, 98],\n",
    "    '接吻镜头': [104, 100, 81, 10, 5, 2],\n",
    "    '电影类型': ['爱情片','爱情片','爱情片','动作片','动作片','动作片']\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "new_data=[18,90]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify0(inX, dataSet, k):\n",
    "    # 不同算法此处 计算方式不同\n",
    "    dist=(((df.iloc[:,1:3] - new_data)**2).sum(axis=1))**.5\n",
    "    dist_1 = pd.DataFrame({\n",
    "        'dist' : dist,\n",
    "        'label': dataSet.iloc[:,-1]\n",
    "    })\n",
    "    dist_s = dist_1.sort_values(by='dist').iloc[:k]    \n",
    "    re = dist_s.value_counts('label')\n",
    "    return re.index[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'爱情片'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify0([18,90], df, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "label\n",
       "爱情片    3\n",
       "动作片    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def classify0(intX, dataSet, k):\n",
    "    (dataSet[:,1:3] - inX)**2\n",
    "    return result\n",
    "dist=(((df.iloc[:,1:3] - new_data)**2).sum(axis=1))**.5\n",
    "dist_1 = pd.DataFrame({\n",
    "    'dist' : dist,\n",
    "    'label': df.iloc[:,-1]\n",
    "})\n",
    "re = (dist_1.sort_values(by='dist').iloc[:5]).value_counts('label')\n",
    "re "
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6dc62afd8b03c17538a9dfce2fcb18f62cec380cc7b77050462a64b7e4e4814"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 32-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
